/*
 * $Id: debug.h 121 2005-05-16 19:27:17Z bmoore $
 *
 * Copyright (c) 2004, 2005  Branden J. Moore.
 *
 * This file is part of MacBiff, and is free software; you can redistribute
 * it and/or modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * MacBiff is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with MacBiff; if not, write to the Free Software Foundation, Inc., 59
 * Temple Place, Suite 330, Boston, MA  02111-1307 USA.
 *
 */

/*
 * Debugging routines
 *  - Defines:  dprintf(), alert(), error(), derror(), werror()
 */

#ifndef DEBUG_H_
#define DEBUG_H_

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#ifndef EBUG
#  define EBUG 0
#endif

#define FULL_DEBUG	0x00000001
#define NETWORK_DEBUG	0x00000002
extern int print_debug;
extern FILE *_dbgfp;

#define __display(type, ...) \
		fprintf(_dbgfp,  "%s: [%s:%d] ", \
				type, __FILE__, __LINE__ ); \
		fprintf(_dbgfp, ##__VA_ARGS__); \
		fflush(_dbgfp);


#define alert(...) \
	do { \
		__display("ALERT", ##__VA_ARGS__); \
	} while (0)

#define error(...) \
	do { \
		__display("ERROR", ##__VA_ARGS__); \
		abort(); \
	} while (0)

#define derror(str) error(str ": %s\n", strerror(errno))
#define werror(str) alert(str ": %s\n", strerror(errno))

#define dprintf(...) \
	if ( (EBUG) || (print_debug & FULL_DEBUG) ) do { \
		__display("DEBUG", ##__VA_ARGS__); \
		fflush(_dbgfp); \
	} while(0)

#endif /* DEBUG_H_ */
